<?php

class M_public_function extends CI_Model {

	function __construct(){
		parent::__construct();
	}


	function add_to_menu($menu,$item) {
		if(isset($menu['children'])) {

			foreach($menu['children'] as &$child) {
				if($item['parent'] == $child['id']) {
					$child['children'][] = $item;
				} else {
					$this->add_to_menu(&$child,$item);
				}
			}
		}
	}

	function hari_ina($dayname){
		$dayname=strtolower($dayname);
		switch($dayname){
			case 'sunday': return 'Minggu';break;
			case 'monday': return 'Senin';break;
			case 'tuesday': return 'Selasa';break;
			case 'wednesday': return 'Rabu';break;
			case 'thursday': return 'Kamis';break;
			case 'friday': return 'Jumat';break;
			case 'saturday': return 'Sabtu';break;
		}
	}

	function render_menu($menu) {
		//var_dump($menu);
		if(isset($menu['children'])) {
			echo '<ul ';
			if(!isset($menu['level'])){
				echo ' class="sf-menu"';
			}

			echo ' >';
			foreach($menu['children'] as &$child) {
				echo "<li ";
				if(!isset($menu['level'])){
					echo ' class="current"';
				}
				echo ">";
				if($child['link']!=="#"){

					echo "<a href='".base_url().$child['link']."' >".$child['menu']."</a>";
				}else{
					echo "<a href='#' >".$child['menu']."</a>";
				}
				if(isset($child['children'])) {
					$this->render_menu(&$child);
				}
				echo "</li>";
			}
			echo '</ul>';
		}
	}

	function get_menu($parentid){
		$menu=array();

		if($parentid==1){
			$where_parent=" parent<>2 AND parent <>3 ";
		}elseif($parentid==2){
			$where_parent=" parent<>1 AND parent <>3 ";
		}elseif($parentid==3){
			$where_parent=" parent<>2 AND parent <>1 ";
		}else{
			$where_parent=" parent =".$parentid;
		}

		$sql="SELECT id, menu, link, parent, level, urut FROM menu
							 WHERE level>0 AND ".$where_parent."
							 ORDER BY parent ASC, urut ASC";
		$query = mysql_query($sql);
		//$this->firephp->log($sql);

		if(mysql_numrows($query)!= 0) {
				while($row = mysql_fetch_assoc($query)) {
					if($row['level']==1)
						$menu['children'][] = $row;
					else
						$this->add_to_menu(&$menu,$row);
				}
		}

		//var_dump($this->menu);
		$this->render_menu($menu);
	}

	function render_left_menu($menu) {
		//var_dump($menu);

		if(isset($menu['children'])) {
			//echo $i;

			foreach($menu['children'] as &$child) {
				if($child['link']=='#') {
					echo "<a headerindex=\"".$i."h\" class=\"menuitem submenuheader headeractive\" href=\"#\" title=\"".$child['menu']."\">";
					echo "<span class=\"accordprefix\"></span>".$child['menu']."<span class=\"accordsuffix\">";
					echo "<img src=\"../images/arrow_up.jpg\" class=\"statusicon\"></span></a>";
					echo "<div style=\"display: block;\" contentindex=\"".$i."c\" class=\"submenu\">";
					echo '<ul>';
					if(isset($child['children'])) {
						$this->render_left_menu(&$child);
					}
					echo "</ul></div>";


				}elseif($child['link']!=='#'){

					echo "<li>";
					if($child['link']!=="#"){
						echo "<a href='".base_url().$child['link']."' >".$child['menu']."</a>";
					}else{
						echo "<a href='#' >".$child['menu']."</a>";
					}
					if(isset($child['children'])) {
						$this->render_left_menu(&$child);
					}
					echo "</li>";
				}
			}


		}

	}

	function get_left_menu($parentid){
		$menu=array();

		if($parentid==1){
			$where_parent=" parent<>2 AND parent <>3 ";
		}elseif($parentid==2){
			$where_parent=" parent<>1 AND parent <>3 ";
		}elseif($parentid==3){
			$where_parent=" parent<>2 AND parent <>1 ";
		}else{
			$where_parent=" ( (path LIKE '%/".$parentid."/%' AND link<>'#') OR  id=".$parentid.")";
		}

		if($_SESSION[SESSION_PAGE]==1){
			$where_parent.="  AND keterangan='CW'";
		}else if($_SESSION[SESSION_PAGE]==2){
			$where_parent.="  AND keterangan='IR'";
		}elseif($_SESSION[SESSION_PAGE]==3){
			$where_parent.="  AND keterangan='CS'";
		}

		$sql="SELECT id, menu, link, parent, level as lvl, urut FROM menu
							 WHERE level>0 AND ".$where_parent."
							 ORDER BY parent ASC, urut ASC";
		$query = mysql_query($sql);
		//$this->firephp->log($sql);

		$sql="SELECT min(t.lvl) as levelmin FROM (".$sql.") t";
		//$this->firephp->log("level ".$sql);
		$rsmin = mysql_query($sql);
		$level=1;
		if(mysql_numrows($rsmin)>0){
			while($lvl=mysql_fetch_assoc($rsmin)){
				$level=$lvl['levelmin'];
			}

		}

		//$this->firephp->log("level : ".$level);

		if(mysql_numrows($query)) {

				while($row = mysql_fetch_assoc($query)) {
					if($row['lvl']==$level){
						$menu['children'][] = $row;
					}
					else{
						$this->add_to_menu(&$menu,$row);
					}
				}
		}

		//var_dump($menu);
		$this->render_left_menu($menu);

	}


	function get_page($permalink){

		$sql="SELECT * FROM page WHERE permalink='$permalink'";
		$rs=$this->db->query($sql);
		if($rs->num_rows()){
			return $rs->row();
		}else{
			return "";
		}
	}

	function get_awards_page(){
		$sql="SELECT * FROM awards WHERE publish='y'";
		$rs=$this->db->query($sql);
		if($rs->num_rows()){
			return $rs->result();
		}else{
			return "";
		}
	}

	function get_promotions_page(){
		$sql="SELECT * FROM promotions WHERE publish='y'";
		$rs=$this->db->query($sql);
		if($rs->num_rows()){
			return $rs->result();
		}else{
			return "";
		}
	}

	function get_kurs_page(){
		$sql="SELECT * FROM kurs";
		$rs=$this->db->query($sql);
		if($rs->num_rows()){
			return $rs->result();
		}else{
			return "";
		}
	}

	function get_kurs_tanggal(){
		$sql="SELECT distinct date_format(tanggal,'%d-%m-%Y') tanggal FROM kurs";
		$rs=$this->db->query($sql);
		if($rs->num_rows()){
			$row=$rs->row();
			return $row->tanggal;
		}else{
			return "";
		}
	}

	function get_highlightpage(){

		$sql="SELECT * FROM highlight WHERE publish='y'";
		$rs=$this->db->query($sql);
		if($rs->num_rows()){
			return $rs->result();
		}else{
			return "";
		}
	}

	function get_menu_home(){
		$sql="SELECT id, menu, link FROM menu WHERE level=0 ORDER BY urut ASC";
		$rs=$this->db->query($sql);
		if($rs->num_rows()){
			return $rs->result();
		}else{
			return "";
		}
	}

	function get_menu_utama(){
		$sql="SELECT id, menu, link FROM menu WHERE level=0 ORDER BY urut ASC";
		$rs=$this->db->query($sql);
		if($rs->num_rows()){
			return $rs->result();
		}else{
			return "";
		}
	}

	function get_menu_investor(){
		$sql="SELECT id, menu, link FROM menu WHERE level=0 ORDER BY urut ASC";
		$rs=$this->db->query($sql);
		if($rs->num_rows()){
			return $rs->result();
		}else{
			return "";
		}
	}

	function get_cicilan_efektif($nilai, $bunga, $tenor){
		$cicilan=array();
		$pokok=$nilai/$tenor;
		for($i=1;$i<=$tenor;$i++){
			$cicilan[$i]['pokok']=$pokok;
			$cicilan[$i]['bunga']=($nilai-($pokok*$i))*$bunga/100*(30/360);
			$cicilan[$i]['bayar']=$pokok+($nilai-($pokok*$i))*$bunga/100*(30/360);
		}
		return $cicilan;
	}

	function get_cicilan_flat($nilai, $bunga, $tenor){
		$cicilan=array();
		$pokok=$nilai/$tenor;
		$bunga_rp=$nilai*$bunga/100/$tenor;
		for($i=1;$i<=$tenor;$i++){
			$cicilan[$i]['pokok']=$pokok;
			$cicilan[$i]['bunga']=$bunga_rp;
			$cicilan[$i]['bayar']=$pokok+$bunga_rp;
		}
		return $cicilan;
	}

	function get_menuparent_berita($judul){

		$sql="select parent,menu
			FROM menu
			INNER JOIN berita
			ON( substr(link,12,LENGTH(link)-16)=replace(lower(judul),' ','_'))
			WHERE judul='$judul'
			ORDER BY urut ASC";

		$rs=$this->db->query($sql);
		if($rs->num_rows()){
			$link=$rs->row();
			return $link->parent;
		}else{
			return "";
		}

	}

	function get_menuparent_by_url($link){

		$sql="SELECT parent,menu
			FROM menu
			WHERE link='$link' ";


		if($_SESSION[SESSION_PAGE]==1){
			$sql.="  AND keterangan='CW'";
		}else if($_SESSION[SESSION_PAGE]==2){
			$sql.="  AND keterangan='IR'";
		}elseif($_SESSION[SESSION_PAGE]==3){
			$sql.="  AND keterangan='CS'";
		}

		$sql.="ORDER BY urut ASC";
		//$this->firephp->log($sql);
		//echo $sql;
		$rs=$this->db->query($sql);
		if($rs->num_rows()){
			$link=$rs->row();
			return $link->parent;
		}else{
			return "";
		}

	}

	function get_menuparent_by_pages($judul){

		$sql="select parent,menu
			FROM menu
			INNER JOIN page
			ON( substr(link,12,LENGTH(link)-16)=replace(lower(judul),' ','_'))
			WHERE judul='$judul'
			ORDER BY urut ASC";
		//$this->firephp->log($sql);
		//echo $sql;
		$rs=$this->db->query($sql);
		if($rs->num_rows()){
			if($rs->num_rows()>1){
				$link=$rs->result();
				return $link['parent'][$_SESSION[SESSION_PAGE]];
			}else{
				$link=$rs->row();
				return $link->parent;
			}

		}else{
			return "";
		}

	}

	function get_menu_tentang(){

		$sql="SELECT id, menu, link, parent, level, urut FROM menu
							 WHERE level>0 AND parent=79
							 ORDER BY parent ASC, urut ASC";
		$rs=$this->db->query($sql);
		if($rs->num_rows()){
			return $rs->result();
		}else{
			return "";
		}
	}

	function get_menu_social(){
		$sql="SELECT id, menu, link FROM menu WHERE level=0 ORDER BY urut ASC";
		$rs=$this->db->query($sql);
		if($rs->num_rows()){
			return $rs->result();
		}else{
			return "";
		}
	}
	function get_count_rows($sql){
		$sqli="";
		$sqls="";
		$split_order=preg_split("/ORDER/i",$sql);
		$sqls=$split_order[0];
		$split_sql=preg_split("/FROM/i",$sqls);
		$sqli.="SELECT count(1) as jumlah ";
		for($i=1;$i<sizeof($split_sql);$i++){
			$sqli.=' FROM '.$split_sql[$i];
		}
		$rs=$this->db->query($sqli);
		if($rs->num_rows()){
			$row=$rs->row();
			return $row->jumlah;
		}else{
			return 0;
		}
	}

	function get_tgl_mysql($tgl){

		if($tgl<>""){
			$tanggal=split("/",$tgl);
			return $tanggal[2]."-".$tanggal[1]."-".$tanggal[0];
		}

	}

	function get_tgl_ina($tgl){

		if($tgl<>""){
			$tanggal=split("-",$tgl);
			return $tanggal[2]."/".$tanggal[1]."/".$tanggal[0];
		}

	}

	function get_tgl_huruf($tgl){

		$bulan=array("Januari","Pebruari","Maret","April","Mei",
					  "Juni","Juli","Agustus","September",
					  "Oktober","Nopember","Desember");
		if($tgl<>""){
			$tanggal=split("-",$tgl);
			return $tanggal[2]." ".$bulan[(int)$tanggal[1]]." ".$tanggal[0];
		}
	}

	function get_captcha(){
		$this->load->helper('captcha');
		$vals = array(
			'img_path' => './captcha/',
			'img_url' => './captcha/',
			'img_width' => 150,
			'img_height' => 60,
			'font_path' => './assets/fonts/BAUHS93.ttf',
			'expiration' => 7200,
			);

		$cap = create_captcha($vals);

		$data = array(
			'captcha_time' => $cap['time'],
			'ip_address' => $this->input->ip_address(),
			'word' => $cap['word']
			);

		$query = $this->db->insert_string('captcha', $data);
		$this->db->query($query);
		return $cap["image"];
	}

	function cek_keycode($keycode, $ipAddr){

		$expiration = time()-4200 ;
		$sql = "DELETE FROM captcha WHERE captcha_time < $expiration";
		$rs=$this->db->query($sql);

		$sql="SELECT * FROM captcha WHERE ip_address='".$ipAddr."'
				AND word='".$keycode."' LIMIT 1";
		$rs=$this->db->query($sql);
		if($rs->num_rows()){

			$sql="DELETE  FROM captcha WHERE ip_address='".$ipAddr."'
				AND word='".$keycode."'";
			$result=$this->db->query($sql);
			if($this->db->affected_rows()){
				return 1;
			}else{
				return 0;
			}

		}else{

			return 0;
		}

	}

	function enkrip($key, $string){
		return base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($key), $string, MCRYPT_MODE_CBC, md5(md5($key))));
	}

	function cek_aktivasi($kode_aktivasi){
		$sql="SELECT user_confirm FROM takita_user WHERE user_confirm='".$kode_aktivasi."' LIMIT 1";
		//$this->firephp->log($sql);
		//echo $sql;

		$rs=$this->db->query($sql);
		if($rs->num_rows()){

			$sql="UPDATE takita_user SET user_aktif='y' WHERE user_confirm='".$kode_aktivasi."'";
			$rs=$this->db->query($sql);
			return 1;

		}else{

			return -1;
		}
	}

	function get_nama_by_userid($userid){

		$sql="SELECT user_nama FROM takita_user WHERE md5(user_id)='".$userid."'";
		$rs=$this->db->query($sql);
		if($rs->num_rows()){
			$row=$rs->row();
			return $row->user_nama;
		}else{
			return "";
		}


	}

	function get_email_by_userid($userid){

		$sql="SELECT user_id FROM takita_user WHERE md5(user_id)='".$userid."'";
		$rs=$this->db->query($sql);
		if($rs->num_rows()){
			$row=$rs->row();
			return $row->user_id;
		}else{
			return "";
		}


	}

	function get_tantangan(){
		$sql="SELECT * FROM takita_tantangan ORDER BY tantangan_id LIMIT 1";
		$rs=$this->db->query($sql);
		if($rs->num_rows()){
			$row=$rs->row();
			return $row;
		}else{
			return "";
		}
	}


	function get_ikut($userid){
		$sql="SELECT * FROM takita_ikut, takita_tantangan
				WHERE md5(ikut_userid)='$userid'
				AND	ikut_tantangan=tantangan_id
				ORDER BY ikut_tantangan DESC LIMIT 1";
		$rs=$this->db->query($sql);
		if($rs->num_rows()){
			$row=$rs->result();
			return $row;
		}else{
			return "";
		}
	}

	function get_kota_by_userid($userid){

		$sql="SELECT kota_nama FROM takita_profil, takita_kota
				WHERE 	profil_kota=kota_id
				AND 	md5(profil_userid)='".$userid."'";
		$rs=$this->db->query($sql);
		if($rs->num_rows()){
			$row=$rs->row();
			return $row->kota_nama;
		}else{
			return "";
		}


	}

	function get_profil_by_userid($userid){

		$sql="SELECT * FROM takita_profil
				WHERE 	md5(profil_userid)='".$userid."' LIMIT 1--";
		$rs=$this->db->query($sql);
		if($rs->num_rows()){
			$row=$rs->row();
			return $row;
		}else{
			return "";
		}

	}

	function get_detail_by_userid($userid){

		$sql="SELECT profil_bio FROM takita_profil
				WHERE 	md5(profil_userid)='".$userid."'";
		$rs=$this->db->query($sql);
		if($rs->num_rows()){
			$row=$rs->row();
			return $row->profil_bio;
		}else{
			return "";
		}


	}

	function get_register_by_userid($userid){

		$sql="SELECT user_register FROM takita_user
				WHERE 	md5(user_id)='".$userid."'";
		$rs=$this->db->query($sql);
		if($rs->num_rows()){
			$row=$rs->row();
			return $row->user_register;
		}else{
			return "";
		}
	}
}